package com.msoe.client.panels;

import java.util.HashMap;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.msoe.client.Session;
import com.msoe.client.panels.eventhandlers.RegisterEventHandler;

public class Register implements MSOERPanel{

	public static final String DIV_ID = "register";

	private Panel registerPanel;
	private Session session;
	private DecoratorPanel decoration;

	// TextBox's used to register a user
	private TextBox T_First_Name;
	private TextBox T_Last_Name;
	private TextBox T_UserName;
	private TextBox T_Email;

	public Register(Session session){

		this.session = session;

		registerPanel = new VerticalPanel();
		decoration = new DecoratorPanel();

		HorizontalPanel TitlePanel = new HorizontalPanel();
		HorizontalPanel FirstNamePanel = new HorizontalPanel();
		HorizontalPanel LastNamePanel = new HorizontalPanel();
		HorizontalPanel UserNamePanel = new HorizontalPanel();
		HorizontalPanel EmailPanel = new HorizontalPanel();
		HorizontalPanel ButtonPanel = new HorizontalPanel();

		T_First_Name = new TextBox();
		T_Last_Name = new TextBox();
		T_UserName = new TextBox();
		T_UserName.setReadOnly(true);
		T_Email = new TextBox();
		Button B_submit = new Button("Submit");
		Button B_cancel = new Button("Cancel");
		Label L_title = new Label("MSOE Ride Registration");
		L_title.addStyleDependentName("LargeText");
		Label L_First_Name = new Label("First Name:");
		Label L_Last_Name = new Label("Last Name:");
		Label L_Username = new Label("Username:");
		Label L_Email = new Label("Email:");

		//Assemble horizontal panels
		TitlePanel.setSpacing(5);
		TitlePanel.add(L_title);

		FirstNamePanel.setSpacing(5);
		FirstNamePanel.add(L_First_Name);
		FirstNamePanel.add(T_First_Name);
		
		LastNamePanel.setSpacing(5);
		LastNamePanel.add(L_Last_Name);
		LastNamePanel.add(T_Last_Name);

		UserNamePanel.setSpacing(5);
		UserNamePanel.add(L_Username);
		UserNamePanel.add(T_UserName);

		EmailPanel.setSpacing(5);
		EmailPanel.add(L_Email);
		EmailPanel.add(T_Email);

		ButtonPanel.setSpacing(10);
		ButtonPanel.add(B_submit);
		ButtonPanel.add(B_cancel);

		registerPanel.add(TitlePanel);
		registerPanel.add(FirstNamePanel);
		registerPanel.add(LastNamePanel);
		registerPanel.add(EmailPanel);
		registerPanel.add(UserNamePanel);
		registerPanel.add(ButtonPanel);

		RegisterEventHandler handler = new RegisterEventHandler(this);
		T_Email.addKeyboardListener(handler);
		B_submit.addClickListener(handler);
		B_cancel.addClickListener(handler);

		decoration.add(registerPanel);
	}
	
	public HashMap<String, String> getFields(){
		HashMap<String, String> fields = new HashMap<String, String>();
		
		fields.put("fname", T_First_Name.getText());
		fields.put("lname", T_Last_Name.getText());
		fields.put("username",T_UserName.getText());
		fields.put("email",T_Email.getText());
		
		return fields;
	}
	
	public TextBox getUserNameBox(){
		return T_UserName;
	}

	public void show(){
		if(!session.validate()){
			clear();
			RootPanel.get().clear();
			RootPanel.get(DIV_ID).add(decoration);
		}
		else{
			PanelMap.getPanel("UserHome").show();
		}
	}

	public void hide(){
		decoration.removeFromParent();
	}

	private void clear(){
		T_First_Name.setText("");
		T_Last_Name.setText("");
		T_UserName.setText("");
		T_Email.setText("");
	}

	public Session getSession() {
		return session;
	}
}
